const url = require("url");

const path = require("path");

const qs = require("querystring");

const fs = require("fs");


const urlStr = "http://www.baidu.com:80/abc/qwe/index.html?username=admin&password=124#top";
// 将url字符解析成url对象
console.log( url.parse(urlStr, true) );

const urlObj = {
    protocol: 'http:',
    slashes: true,
    auth: null,
    host: 'www.baidu.com:80',
    port: '80',
    hostname: 'www.baidu.com',
    hash: '#top',
    search: '?username=admin&password=124',
    query: { username: 'admin', password: '124' },
    pathname: '/abc/qwe/index.html',
    path: '/abc/qwe/index.html?username=admin&password=124',
    href: 'http://www.baidu.com:80/abc/qwe/index.html?username=admin&password=124#top'
}
// 将url对象解析成url字符
console.log( url.format(urlObj) );

// ============

// 自动根据当前操作系统的支持，拼接路径
// windows:abc\\qwe\\asd\\zxc\\index.html
// mac:abc/qwe/asd/zxc/index.html
console.log( path.join("abc","qwe","asd","zxc","index.html") );

// ==============

const queryStr = "name=%E5%BC%A0%E4%B8%89&sex=%E7%94%B7&age=19";
// 将query字符解析成对象
console.log( qs.parse(queryStr) );

// 将对象解析成query字符
const obj = {name:"张三",sex:"男",age:19}
console.log( qs.stringify(obj) );

// 对中文或特殊字符进行编码
console.log(qs.escape("https://www.baidu.com/s?wd=上海"));
// 反编码成中文或特殊字符
console.log(qs.unescape("https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%E4%B8%8A%E6%B5%B7"));


// =============

// 读取文件内容
fs.readFile("./hello.txt", "utf-8", (error, data)=>{
    console.log(error);
    console.log(data);
})
// 写入文件内容
fs.writeFile("./world.txt", "哈哈哈哈", error=>{
    console.log(error);
})

// 改名
fs.rename("./hello.txt", "./hahaha.html", error=>{
    console.log(error);
})

// =========

console.log( "__dirname:start" );
// 获取当前文件所在的文件夹的绝对路径
console.log( __dirname );
// 获取当前文件的绝对路径
console.log( __filename );
console.log( "__dirname:end" );